<?php

class Service_HomeModel
{/*{{{*/

    const STATUS = 1;
    const START = 3;
    const SCORE_NOMAL = 90;

    public function homeworkto($uid = 0, $stuid = 0, $times = '', $opernid = 0, $sectionid = 0, $number = 0, $type = 0, $ask = '')
    {
        if (!$uid || !$stuid || !$times || !$opernid || !$sectionid || !$number || !$type || !$ask) throw new YExceptionTool(YExceptionTool::PARAMPACK_ERROR);

        $sectionids = explode(',', $sectionid);
        $stuids = explode(',', $stuid);
        $times = explode(',', $times);

        $homeworkdata = array();
        $homeworkdata['mid'] = $opernid;
        $homeworkdata['type'] = $type;
        $homeworkdata['ask'] = $ask;

        $_homeworkDao = new Dao_StudenthomeworkModel();
        $_homeworkdateDao = new Dao_StudenthomeworkdateModel();
        $_homeworkcommentDao = new Dao_StudenthomeworkcommentModel();
        foreach ($sectionids as $sectionid)
        {
            if (!$sectionid) continue;
            $homeworkdata['sectionid'] = $sectionid;
            $hid = $_homeworkDao->add($homeworkdata);
            foreach ($stuids as $stuid)
            {
                if (!$stuid) continue;
                foreach ($times as $date)
                {
                    if (!$date) continue;
                    for ($i=1; $i<= $number; $i++)
                    {
                        $homeworkdatedata = array();
                        $homeworkdatedata['date'] = $date;
                        $homeworkdatedata['hid'] = $hid;
                        $homeworkdatedata['sid'] = $stuid;
                        $_homeworkdateDao->add($homeworkdatedata);
                    }

                    $homeworkcomment = array();
                    $homeworkcomment['date'] = $date;
                    $homeworkcomment['uid'] = $uid;
                    $homeworkcomment['sid'] = $stuid;
                    $_homeworkcommentDao->add($homeworkcomment);
                }
            }
        }

        $_musicDao = new Dao_MusicModel();
        $musicinfo = $_musicDao->find($opernid);
        if ($musicinfo && $musicinfo['oid'])
        {
            $_usedopernDao = new Dao_UsedopernModel();
            $data = array();
            $data['uid'] = $uid;
            $data['oid'] = $musicinfo['oid'];
            $_usedopernDao->add($data);
        }

        return true;
    }

    //查看作业
    public function homeworkinfo ($id = 0)
    {
        if (!$id) throw new YExceptionTool(YExceptionTool::HOMEWORKID_ERROR);

        $_homework_dateDao = new Dao_StudenthomeworkdateModel();
        $date_info = $_homework_dateDao->getOne(array('*'), array(array('id', $id)));

        if (!$date_info) throw new YExceptionTool(YExceptionTool::HOMEWORK_NOT_EXITS);

        $_homeworkDao = new Dao_StudenthomeworkModel();
        $homework_info = $_homeworkDao->getOne(array('*'), array(array('id', $date_info['hid'])));

        $_musicDao = new Dao_MusicModel();
        $musicinfo = $_musicDao->getOne(array('*'), array(array('id', $homework_info['mid'])));

        $info = array();
        $info['name'] = isset($musicinfo['name']) ? (string) $musicinfo['name'] : '';
        $info['file'] = isset($date_info['file']) ? (string) $date_info['file'] : '';
        $info['sectionid'] = isset($homework_info['sectionid']) ? intval($homework_info['sectionid']) : 0;
        return $info;
    }

    //批改作业
    public function checkhomework ($id, $flagnum, $comment)
    {
        $data = array();
        $data['flagnum'] = $flagnum;
        $data['comment'] = $comment;
        $_homework_dateDao = new Dao_StudenthomeworkdateModel();
        return $_homework_dateDao->edit($data, $id);
    }

    //作业列表
    public function gethomework ($uid = 0)
    {
        if (!$uid) throw new YExceptionTool(YExceptionTool::UID_ERROR);

        $date = date('Y-m-d', strtotime('+1 day'));

        $_homework_commentDao = new Dao_StudenthomeworkcommentModel();
        $dates = $_homework_commentDao->getRowsDate($uid, $date);
        $dates = array_unique(array_column($dates, 'date'));
        $info = $_homework_commentDao->getRowsInfoByDate($uid, $dates);
        $info = Tools_Utilsareas::indexPull($info, 'date');
        sort($dates);

        $_homework_dateDao = new Dao_StudenthomeworkdateModel();
        $date_info = $_homework_dateDao->getRows(array('*'), array(array('sid', $uid), array('date', $dates)));
        $hids = array_column($date_info, 'hid');
        $date_info = Tools_Utilsareas::indexPullArray($date_info, 'date');

        $_homeworkDao = new Dao_StudenthomeworkModel();
        $homework_info = $_homeworkDao->getRows(array('*'), array(array('id', $hids)));

        $mids = array_column($homework_info, 'mid');
        $homework_info = Tools_Utilsareas::indexPull($homework_info, 'id');

        $_musicDao = new Dao_MusicModel();
        $musicinfo = $_musicDao->getRows(array('*'), array(array('id', $mids)));
        $musicinfo = Tools_Utilsareas::indexPull($musicinfo, 'id');

        $list = array();
        foreach ($dates as $date)
        {
            $one = array();
            $one['date'] = intval(strtotime($date));
            $one['flagnum'] = isset($info[$date]) ? intval($info[$date]['flagnum']) : 0;
            $one['status'] = isset($info[$date]) ? intval($info[$date]['status']) : 0;

            $song_info = array();
            if (isset($date_info[$date]))
            {
                $ask = array();
                foreach ($date_info[$date] as $one_date_info)
                {
                    if (!isset($homework_info[$one_date_info['hid']])) continue;
                    $ask[$one_date_info['hid']] = $homework_info[$one_date_info['hid']]['ask'];

                    if (!isset($musicinfo[$homework_info[$one_date_info['hid']]['mid']])) continue;
                    $one_song = array();
                    $one_song['id'] = intval($one_date_info['id']);
                    $one_song['name'] = (string) $musicinfo[$homework_info[$one_date_info['hid']]['mid']]['name'];
                    $one_song['star'] = intval($one_date_info['star']);
                    $one_song['status'] = intval($one_date_info['status']);
                    $song_info[] = $one_song;

                }
                $one['ask'] = (string) implode(',',  $ask);
            }else{
                $one['ask'] = (string) '';
            }
            $one['commment'] = isset($info[$date]) ? (string) $info[$date]['comment'] : 0;
            $one['flagnum'] = isset($info[$date]) ? intval($info[$date]['flagnum']) : 0;
            $one['list'] = $song_info;
            $list[] = $one;
        }

        $res = array();
        $res['list'] = $list;
        return $res;

    }

    //提交作业
    public function submithomework ($id, $sid, $star, $stime, $etime)
    {
        $data = array();
        $data['star'] = $star;
        $data['status'] = self::STATUS;
        $data['stime'] = $stime;
        $data['etime'] = $etime;
        $data['duration'] = strtotime($etime) - strtotime($stime);

        if (self::START == $star) $this->submithomeworkByStart($id, $sid);

        $upload = new Upload();
        $upload->upload('file');
        $data['file'] = $upload->getFileDir();

        $data['file'] = 'http://aaaaaabc.com';

        $_homework_dateDao = new Dao_StudenthomeworkdateModel();
        return $_homework_dateDao->edit($data, array(array('id', $id)));
    }

    private function submithomeworkByStart($hid, $sid)
    {
        $_homework_dateDao = new Dao_StudenthomeworkdateModel();
        $hid = $_homework_dateDao->find($hid);
        if (!$hid) return;

        $_homeworkDao = new Dao_StudenthomeworkModel();
        $mid = $_homeworkDao->getOne(array('mid'), array(array('id', $hid['hid'])));
        if (!$mid) return;

        $sql = 'select count(*) as cnt from piano_student_homework_date as a left join piano_student_homework as b on a.hid = b.id where a.sid = ' .$sid. ' and b.mid = ' . $mid['mid'] . ' and a.status = 1';
        $res = $_homeworkDao->query($sql);
        if ($res && $res[0]['cnt'] >= 3)
        {
            $_grasp_opernDao = new Dao_StudentgraspopernModel();
            $_cnt = $_grasp_opernDao->getOne(array('*'), array(array('sid', $sid), array('mid', $mid['mid'])));

            if ($_cnt) return;
            $data = array();
            $data['sid'] = $sid;
            $data['mid'] = $mid['mid'];
            $_grasp_opernDao->add($data);
        }
    }

    //测评
    public function exam($sid, $mid, $stime, $etime, $score)
    {
        $data = array();
        $data['sid'] = $sid;
        $data['mid'] = $mid;
        $data['stime'] = $stime;
        $data['etime'] = $etime;
        $data['duration'] = strtotime($etime) - strtotime($stime);
        $data['score'] = $score;

        //$upload = new Upload();
        //$upload->upload('file');
        //$data['file'] = $upload->getFileDir();
        $data['file'] = 'http://abc.com';
        $_homework_dateDao = new Dao_StudenthomeworkexamModel();
        return $_homework_dateDao->addInfo($data);
    }

    private function submithomeworkByScore($sid, $mid)
    {
        $_grasp_opernDao = new Dao_StudentgraspopernModel();
        $_cnt = $_grasp_opernDao->getOne(array('*'), array(array('sid', $sid), array('mid', $mid)));
        if ($_cnt) return;
        $data = array();
        $data['sid'] = $sid;
        $data['mid'] = $mid;
        $_grasp_opernDao->add($data);
    }


    /*
     * 获得用户一首歌最大的星星数
     */

    public function getmusicStar ($uid , $musicid)
    {
        $_homeworkDao = new Dao_StudenthomeworkModel();
        $sql          = 'select max(`star`) as maxstar from piano_student_homework_date as a left join piano_student_homework as b on a.hid = b.id where a.sid = ' . $sid . ' and b.mid = ' . $musicid . ' and a.status = 1';
        $res          = $_homeworkDao->query ( $sql );
        return $res[ 0 ][ 'maxstar' ] ? $res[ 0 ][ 'maxstar' ] : 0;
    }

    /*
     * 练琴报告
     */

    public function getReport ($uid , $sid)
    {
        Service_UserModel::userVail($uid , $sid);
        $report                = array ();
        $_studentgrasp_Dao     = new Dao_StudentgraspopernModel();
        $parsptCount           = count ( $_studentgrasp_Dao->getGraspList ( $uid ) );  //用户掌握曲目数
        $report[ 'masternum' ] = intval ( $parsptCount );
        //练习模式近一个月的练琴报告
        $_homeworkdateDao      = new Dao_StudenthomeworkdateModel();
        $date_info             = $_homeworkdateDao->getHomeworkdata ( $uid );
        $_homeworkDao = new Dao_StudenthomeworkModel();
        $_musicDao = new Dao_MusicModel();
        foreach ( $date_info as $datek => $datev )
        {
//            //通过作业id找到musicid
            $hid = $datev['hid'];
            $homeworkinfo = $_homeworkDao ->getHomeworkByHid($hid);
            $musicinfo = $_musicDao->getMusic(array($homeworkinfo['mid']));
//            $newdate_info[ $datek ][ 'mid' ]    = $homeworkinfo['mid'];
//            $newdate_info[ $datek ][ 'hid' ]    = $datev['hid'];
            $newdate_info[ $datek ][ 'name' ]    = $musicinfo[0]['name'];  //名字
            $newdate_info[ $datek ][ 'type' ]    = 1;   //类型
            $newdate_info[ $datek ][ 'sumdur' ]  = intval ( $datev[ 'sumdur' ] );  //练习时长
            $newdate_info[ $datek ][ 'starnum' ] = intval ( $datev[ 'maxstar' ] );  //星星数
            $newdate_info[ $datek ][ 'score' ]   = 0;  //练习模式没有成绩
            $newdate_info[ $datek ][ 'date' ]    = $datev[ 'date' ];
            unset($homeworkinfo);
            unset($musicinfo);
        }
        //考评
        $_homeworkexamDao = new Dao_StudenthomeworkexamModel();
        $exam_data        = $_homeworkexamDao->getExamlastMonth ( $uid );
        
        foreach ( $exam_data as $examk => $examv )
        {
            $musicinfo = $_musicDao->getMusic(array($examv['mid']));
            $newexam_data[ $examk ][ 'name' ]    = $musicinfo[0]['name'];
            $newexam_data[ $examk ][ 'type' ]    = 2;
            $newexam_data[ $examk ][ 'sumdur' ]  = intval ( $examv[ 'duration' ] ); //练习时长
            $newexam_data[ $examk ][ 'starnum' ] = 0;
            $newexam_data[ $examk ][ 'score' ]   = intval ( $examv[ 'score' ] );
            $newexam_data[ $examk ][ 'date' ]    = $examv[ 'date' ];
        }
        $newdata = array_merge ( $newdate_info , $newexam_data );
        $newdata = Tools_Utilsareas::indexPullArray ( $newdata , 'date' );
        krsort($newdata);
        foreach ( $newdata as $newdatak => $newdatav )
        {
            $list[ 'time' ]   = strtotime ( $newdatak );
            $list[ 'report' ] = $newdatav;

            $report[ 'list' ][] = $list;
        }
        return $report;
    }

}/*}}}*/